package com.cc.blog.admin.service.impl;

import com.cc.blog.admin.domain.vo.UploadFileRspVO;
import com.cc.blog.admin.service.AdminFileService;
import com.cc.blog.admin.utils.MinioUtil;
import com.cc.blog.common.enums.CodeEnum;
import com.cc.blog.common.exception.BusinessException;
import com.cc.blog.common.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author cc
 * @Date 2024/7/11 9:50
 */
@Service
@Slf4j
@AllArgsConstructor
public class AdminFileServiceImpl implements AdminFileService {

    private final MinioUtil minioUtil;

    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            // 上传文件
            String url = minioUtil.uploadFile(file);

            // 构建成功返参，将图片的访问链接返回
            return Result.success(UploadFileRspVO.builder().url(url).build());
        } catch (Exception e) {
            log.error("==> 上传文件至 Minio 错误: ", e);
            // 手动抛出业务异常，提示 “文件上传失败”
            throw new BusinessException(CodeEnum.FILE_UPLOAD_FAILED);
        }
    }
}
